﻿using LayuiMvc.DataEntity.DataEntity;
using LayuiMvc.Logic.Community;
using LayuiMvcUtil.Exception;
using LayuiMvcUtil.Result;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace LayuiMvc.WebUI.Controllers
{
    public class PostReplyController : Controller
    {
        PostReplyService postReplyService = new PostReplyService();
        #region 帖子回复
        /// <summary>
        /// 帖子回复
        /// </summary>
        /// <param name="saveEntity">帖子回复实体类</param>
        /// <returns></returns>
        public async Task<ActionResult> PostReplyAsync(PostReplyEntity saveEntity)
        {
            saveEntity.ReplyUserId = HttpContext.User.Identity.GetUserId<int>();
            saveEntity.IsAccept = false;
            saveEntity.ReplyTime = DateTime.Now;
            DataResult<int> rtnResult = new DataResult<int>();
            try
            {
                rtnResult.data = await postReplyService.PostReplySaveAsync(saveEntity);
            }
            catch (CustomException exception)
            {
                rtnResult.code = 1;
                rtnResult.msg = exception.Message;
            }
            return Json(rtnResult);
        }
        #endregion

        #region 帖子回复采纳
        /// <summary>
        /// 帖子回复采纳
        /// </summary>
        /// <param name="postId">帖子ID</param>
        /// <param name="replyId">评论ID</param>
        /// <returns></returns>
        public async Task<ActionResult> PostReplyAcceptAsync(int postId,int replyId)
        {
            DataResult<string> rtnResult = new DataResult<string>();
            try
            {
                await postReplyService.PostReplyAcceptAsync(postId, replyId);
            }
            catch (CustomException exception)
            {
                rtnResult.code = 1;
                rtnResult.msg = exception.Message;
            }
            return Json(rtnResult);
        }
        #endregion

        #region 帖子回复删除
        /// <summary>
        /// 帖子回复删除
        /// </summary>
        /// <param name="postId">帖子Id</param>
        /// <param name="replyId">评论Id</param>
        /// <returns></returns>
        public async Task<ActionResult> ReplyDeleteAsync(int postId,int replyId)
        {
            DataResult<string> rtnResult = new DataResult<string>();
            try
            {
                await postReplyService.ReplyDeleteAsync(postId, replyId);
            }
            catch (CustomException exception)
            {
                rtnResult.code = 1;
                rtnResult.msg = exception.Message;
            }
            return Json(rtnResult);
        }
        #endregion
    }
}